
# This Makefile would like Make 2.8 or above

PREFIX=%PREFIX%
LIBDIR=%LIBDIR%
CFLAGS=%CFLAGS%
CXXFLAGS=%CXXFLAGS%

ifneq (%R_PATH%,)
R_PATH=%R_PATH%
endif

ifneq (%BASE_MODULE_PATH%,)
BASE_MODULE_PATH=%BASE_MODULE_PATH%
endif

ifneq (%R_HOME%,)
R_HOME=%R_HOME%
endif

ifeq ($(OS),Windows_NT)
	os = Windows
else
	UNAME_S := $(shell uname -s)
	ifeq ($(UNAME_S),Linux)
		os = Linux
	endif
	ifeq ($(UNAME_S),Darwin)
		os = OSX
	endif
endif

ifeq ($(os),OSX)
	CXX = clang++
endif

SRCDIR ?= $(dir $(firstword $(MAKEFILE_LIST)))
TARGET = jamovi-engine
CXXFLAGS += -std=c++11
INCLUDES += -I.

ifeq ($(os),OSX)
	BINDIR ?= MacOS
	R_HOME ?= $(SRCDIR)/../../../Frameworks/R.framework/Versions/Current/Resources
	BASE_MODULE_PATH ?= $(SRCDIR)/../../modules/base/R
	LDFLAGS +=	-L$(SRCDIR)/../../../Frameworks \
				-L$(R_HOME)/lib \
				-L$(BASE_MODULE_PATH)/RInside/lib \
				-L/usr/local/lib \
				-lboost_filesystem \
				-lboost_system \
				-framework Foundation
	CXXFLAGS += -mmacosx-version-min=10.13
else
ifeq ($(os),Linux)
	BINDIR ?= bin
	R_HOME ?= $(SRCDIR)/../../../lib/R
	BASE_MODULE_PATH ?= $(SRCDIR)/../../modules/base/R
	R_PATH ?= $$ORIGIN/../lib  $$ORIGIN/../Resources/modules/base/R/RInside/lib
	LDFLAGS +=	-L$(R_HOME)/lib \
				-L$(BASE_MODULE_PATH)/RInside/lib \
				-lboost_filesystem \
				-lboost_system \
				-lpthread \
				$(foreach rp,$(R_PATH),-Wl,-rpath,'$(rp)')
else
	BINDIR ?= bin
	R_HOME ?= $(SRCDIR)/../../../Frameworks/R
	BASE_MODULE_PATH ?= $(SRCDIR)/../../modules/base/R
	LDFLAGS +=	-L$(SRCDIR)/../../lib \
				-L$(SRCDIR)/../../lib/libgcc \
				-L$(R_HOME)/bin/x64 \
				-L$(BASE_MODULE_PATH)/RInside/lib/x64 \
				-lboost_filesystem-mgw8-mt-x64-1_74 \
				-lboost_system-mgw8-mt-x64-1_74 \
				-lboost_nowide-mgw8-mt-x64-1_74

	# _WIN32_WINNT is for SHGetKnownFolderPath

	CXXFLAGS +=	-DUNICODE \
				"-D_WIN32_WINNT=_WIN32_WINNT_VISTA" \
				"-DWIN32_LEAN_AND_MEAN" \
				"-fpermissive"
endif
endif

INCLUDES += \
	-I$(SRCDIR)/../../include \
	-I$(SRCDIR)/../../../include \
	-I$(SRCDIR)/../server/jamovi/common \
	-I$(R_HOME)/include \
	-I$(BASE_MODULE_PATH)/RInside/include \
	-I$(BASE_MODULE_PATH)/Rcpp/include \
	-I/usr/share/R/include

LDFLAGS += -lR -lnanomsg -lprotobuf -lRInside

ABS_SRCS  = $(wildcard $(SRCDIR)/engine/*.cpp)
ABS_SRCS += $(wildcard $(SRCDIR)/engine/**/*.cpp)
ABS_SRCS += $(wildcard $(SRCDIR)/../server/jamovi/common/*.cpp)
ABS_SRCS += $(wildcard $(SRCDIR)/../server/jamovi/common/**/*.cpp)
SRCS  = $(subst $(SRCDIR)/,,$(ABS_SRCS))

OBJS += jamovi.pb.cc.o
OBJS += $(SRCS:.cpp=.cpp.o)

ifeq ($(os),OSX)
	ABS_M_SRCS  = $(wildcard $(SRCDIR)/../server/jamovi/common/*.m)
	ABS_M_SRCS += $(wildcard $(SRCDIR)/../server/jamovi/common/**/*.m)
	M_SRCS = $(subst $(SRCDIR)/,,$(ABS_M_SRCS))
	OBJS += $(M_SRCS:.m=.m.o)
endif

$(TARGET): $(OBJS)
	mkdir -p $(@D)
	$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)

jamovi.pb.cc: $(SRCDIR)/../server/jamovi/server/jamovi.proto
	protoc --cpp_out=. --proto_path=$(SRCDIR)/../server/jamovi/server $(SRCDIR)/../server/jamovi/server/jamovi.proto

jamovi.pb.cc.o: jamovi.pb.cc
	mkdir -p $(@D)
	$(CXX) -c -o $@ $^ $(CXXFLAGS) $(INCLUDES)

../server/jamovi/common/%.cpp.o: $(SRCDIR)/../server/jamovi/common/%.cpp
	mkdir -p $(@D)
	$(CXX) -c -o $@ $^ $(CXXFLAGS) $(INCLUDES)

../server/jamovi/common/%.m.o: $(SRCDIR)/../server/jamovi/common/%.m
	mkdir -p $(@D)
	$(CXX) -c -o $@ $^ $(INCLUDES)

engine/%.cpp.o: $(SRCDIR)/engine/%.cpp
	mkdir -p $(@D)
	$(CXX) -c -o $@ $^ $(CXXFLAGS) $(INCLUDES)

all: $(TARGET)

clean:
	rm -f $(TARGET) $(OBJS) jamovi.pb.cc jamovi.pc.h

install:
	mkdir -p $(DESTDIR)$(PREFIX)/$(BINDIR)
	cp $(TARGET) $(DESTDIR)$(PREFIX)/$(BINDIR)

.PHONY:	clean all install
